import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

// 用来存储数据
const state = {
  token: '',
  refreshToken: '',
  routerList: [{
    path: '/log',
    name: '日志管理'
  }]
}
// 响应组件中的事件
const actions = {

}
// 操作数据
const mutations = {
  setToken (state, token) {
    state.token = token
  },
  setRefreshToken (state, token) {
    state.refreshToken = token
  },
  selectMenu (state, val) {
    if (val.name === '日志管理') {} else {
      const result = state.routerList.findIndex(item => item.name === val.name)
      // eslint-disable-next-line no-unused-expressions
      result === -1 ? state.routerList.push(val) : ''
    }
    // sessionStorage.setItem('store', JSON.stringify(this.$store.state))
  },
  closeTab (state, val) {
    console.log(state)
    const result = state.routerList.findIndex(item => item.name === val.name)
    state.routerList.splice(result, 1)
  }
}
// 用来将state数据进行加工
const getters = {

}
// 新建并暴露store
export default new Vuex.Store({
  state,
  actions,
  mutations,
  getters,
  /* vuex数据持久化配置 */
  plugins: [
    createPersistedState({
      // 存储方式：localStorage、sessionStorage、cookies
      storage: window.sessionStorage,
      // 存储的 key 的key值
      key: 'store',
      reducer (state) { // render错误修改
        // 要存储的数据：本项目采用es6扩展运算符的方式存储了state中所有的数据
        return { ...state }
      }
    })
  ]
})
